//
// Created by jinbang.li on 2023/8/5.
//

#include "IOpPlayer.h"
#include "../Demux/IOpDemux.h"
#include "../Decode/IOpDecode.h"
#include "../Audio/IOpResample.h"
#include "../Audio/IOpAudioPlay.h"
#include "../Video/IOpVideoView.h"
#include "../Log/OpLog.h"
IOpPlayer* IOpPlayer::get(unsigned char index){
    static IOpPlayer p[256];
    return &p[index];
}
bool IOpPlayer::re_open(const char *path){
    if (!demux || !demux->Open(path)){
        OPLOGE("re_demux->open %s failed",path);
        return false;
    }else{
        OPLOGI("re_demux->open success");
    }
    return true;
}

bool IOpPlayer::open(const char *path){
    //解封装
    if(!demux || !demux->Open(path)){
        OPLOGE("demux->open %s failed",path);
        return false;
    } else{
        OPLOGI("demux->open success");
    }
    //解码
    if (!vdecode || !vdecode->Open(demux->GetVideoParam(),isHardDecode)){
        OPLOGE("vdecode->open %s failed",path);
        return false;
    } else{
        OPLOGI("vdecode->open success");
    }
    if (!adecode || !adecode->Open(demux->GetAudioParam())){
        OPLOGE("adecode->open %s failed",path);
        return false;
    }else{
        OPLOGI("adecode->open success");
    }
    if (outPara.sample_rate <= 0) outPara = demux->GetAudioParam();
    if (!resample || !resample->Open(demux->GetAudioParam(),outPara))
    {
        OPLOGE("resample->open %s failed",path);
    } else{
        OPLOGI("resample->open success");
    }
    return true;
}
bool IOpPlayer::start(){
    if (!demux || !demux->Start()){
        OPLOGE("demux start failed");
        return false;
    } else{
        OPLOGI("demux start success");
    }
    if (adecode){
        bool adecodeFlag = adecode->Start();
        OPLOGI("adecodeFlag === %d",adecodeFlag);
    }

    if (audioPlay){
       bool audioPlayFlag = audioPlay->startPlay(outPara);
        OPLOGI("audioPlayFlag === %d",audioPlayFlag);
    }

    if (vdecode){
        bool vdecodeFlag = vdecode->Start();
        OPLOGI("vdecodeFlag === %d",vdecodeFlag);
    }
    return true;
}
void IOpPlayer::InitView(void *win) {
    if (videoView)
        videoView->SetRender(win);
}